package GameServer;

import Library.*;
import Library.Game.*;
import Library.Login.*;
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;

/**
 * Gateway naar GameServer, omdat Threads niet Serializable zijn. 
 */
public class Gateway extends UnicastRemoteObject implements IGameServerGateway
{
    // Gateway to LoginServer
    /**
     *
     */
    protected transient GameServer m_GameServer;
    
    /**
     *
     * @throws RemoteException
     */
    public Gateway() throws RemoteException
    {
    }
    
    /**
     *
     * @param server
     */
    public void SetGameServer(GameServer server)
    {
        m_GameServer = server;
    }
    
    public ActionResult<IGameInfo> createGame(String name, int maxplayers, int mapid, int gametime, int turntime, int armysize)
        throws RemoteException
    {
        try
        {
            return m_GameServer.createGame(name, maxplayers, mapid, gametime, turntime, armysize);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return new ActionResult<IGameInfo>(ErrorCodes.ERROR_UNKNOWN, null);
    }

    public List<IGameInfo> getGameInfos() throws RemoteException, Exception
    {
        return m_GameServer.getGameInfos();
    }

    public String getIP() throws UnknownHostException, RemoteException
    {
        try
        {
            return m_GameServer.getIP();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }

    public int getPort() throws RemoteException
    {
        try
        {
            return m_GameServer.getPort();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return 0;
    }

    public ActionResult<ISession> join(IGameInfo info, ITicket ticket, IClientService callback) throws RemoteException, Exception
    {
        try
        {
            return m_GameServer.join(info, ticket, callback);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return new ActionResult<ISession>(ErrorCodes.ERROR_UNKNOWN, null);
    }
    
    public IGameInfo findGame(String name) throws RemoteException, Exception
    {
        try
        {
            return m_GameServer.findGame(name);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
}
